#region Copyright Notice
/*
Copyright (C) 2011 by Ashley Ross

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion Copyright Notice

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Manta.IO;

namespace Manta.UnitTests
{
    [TestClass]
    public class JunctionTest
    {
        private static DirectoryInfo _tempRootDir;
        private static string _fakeTarget;
        private static string _realTarget;
        private static string _fakeAlias;
        private static string _realAlias;
        private static string _printName;
        private static string _garbageName;
        private static string _rootName;
        private static string _fatName;

        [ClassInitialize]
        public static void Initialize(TestContext context)
        {
            _tempRootDir = new DirectoryInfo(Path.GetTempPath()).CreateSubdirectory("JunctionTest" + DateTime.Now.Ticks.ToString());

            _fakeTarget = Path.Combine(_tempRootDir.FullName, "fakeTarget");
            _realTarget = Path.Combine(_tempRootDir.FullName, "realTarget");
            _fakeAlias = Path.Combine(_tempRootDir.FullName, "fakeAlias");
            _realAlias = Path.Combine(_tempRootDir.FullName, "realAlias");
            _printName = "Print name. :)";
            _garbageName = "~!@#$%^&*()_+";
            _rootName = @"C:\";

            // Attempt to find a FAT filesystem to test against
            foreach (var drive in System.IO.DriveInfo.GetDrives())
            {
                if (drive.IsReady && drive.DriveFormat.StartsWith("FAT", StringComparison.OrdinalIgnoreCase))
                {
                    _fatName = Path.Combine(drive.RootDirectory.FullName, "alias");
                    break;
                }
            }

            Directory.CreateDirectory(_realTarget);
            File.CreateText(Path.Combine(_realTarget, "test.file")).Close();
        }


        [TestMethod]
        public void CreateWithInvalidParams()
        {
            ExceptionAssert.Throws<DirectoryNotFoundException>(() => Junction.Create(_fakeAlias, _fakeTarget));
            ExceptionAssert.Throws<ArgumentNullException>(() => Junction.Create(_fakeAlias, null));
            ExceptionAssert.Throws<ArgumentNullException>(() => Junction.Create(null, _fakeTarget));
            ExceptionAssert.Throws<ArgumentException>(() => Junction.Create(_garbageName, _fakeTarget));
            ExceptionAssert.Throws<ArgumentException>(() => Junction.Create(_fakeAlias, _garbageName));
            ExceptionAssert.Throws<ArgumentException>(() => Junction.Create(_rootName, _fakeTarget));
            if (!string.IsNullOrEmpty(_fatName))
                ExceptionAssert.Throws<NotSupportedException>(() => Junction.Create(_fatName, _fakeTarget));
        }

        [TestMethod]
        public void ExistsWithInvalidParams()
        {
            Assert.IsFalse(Junction.Exists(_fakeAlias));
            Assert.IsFalse(Junction.Exists(_garbageName));
            Assert.IsFalse(Junction.Exists(_rootName));
            Assert.IsFalse(Junction.Exists(_fatName));
        }

        [TestMethod]
        public void GetWithNonExistingDir()
        {
            JunctionInfo junction = Junction.Get(_fakeAlias);

            Assert.IsFalse(junction.Exists, "junction.Exists returned true.");
            Assert.AreEqual(junction.PrintName, string.Empty);
            Assert.AreEqual(junction.Alias.FullName, _fakeAlias);
            Assert.AreEqual(junction.Target, null);
        }

        [TestMethod]
        public void GetWithGarbageName()
        {
            ExceptionAssert.Throws<ArgumentException>(() => Junction.Get(_garbageName));
        }

        [TestMethod]
        public void GetWithRootName()
        {
            JunctionInfo junction = Junction.Get(_rootName);

            Assert.IsFalse(junction.Exists, "junction.Exists returned true.");
            Assert.AreEqual(junction.PrintName, string.Empty);
            Assert.AreEqual(junction.Alias.FullName, _rootName);
            Assert.AreEqual(junction.Target, null);
        }

        [TestMethod]
        public void GetWithFatName()
        {
            if (!string.IsNullOrEmpty(_fatName))
            {
                JunctionInfo junction = Junction.Get(_fatName);

                Assert.IsFalse(junction.Exists, "junction.Exists returned true.");
                Assert.AreEqual(junction.PrintName, string.Empty);
                Assert.AreEqual(junction.Alias.FullName, _fatName);
                Assert.AreEqual(junction.Target, null);
            }
        }

        [TestMethod]
        public void DeleteWithInvalidParams()
        {
            ExceptionAssert.Throws<DirectoryNotFoundException>(() => Junction.Delete(_fakeAlias));
            ExceptionAssert.Throws<ArgumentException>(() => Junction.Delete(_garbageName));
            ExceptionAssert.Throws<ArgumentNullException>(() => Junction.Delete((string)null));
            ExceptionAssert.Throws<IOException>(() => Junction.Delete(_rootName));
        }

        [TestMethod]
        public void FullLifeCycle()
        {
            try
            {
                Junction.Create(_realAlias, _realTarget, _printName);

                Assert.IsTrue(Directory.Exists(_realAlias));
                Assert.AreNotEqual(0, Directory.GetFiles(_realAlias).Length);


                Assert.IsTrue(Junction.Exists(_realAlias));


                JunctionInfo junction = Junction.Get(_realAlias);

                Assert.IsTrue(junction.Exists, "junction.Exists returned false.");
                Assert.AreEqual(junction.PrintName, _printName);
                Assert.AreEqual(junction.Alias.FullName, _realAlias);
                Assert.AreEqual(junction.Target.FullName, _realTarget);


                Junction.Delete(_realAlias);
                Assert.IsFalse(Directory.Exists(_realAlias));
            }
            finally
            {
                if (Directory.Exists(_realAlias))
                    Directory.Delete(_realAlias, false);
            }
        }


        [ClassCleanup]
        public static void Cleanup()
        {
            _tempRootDir.Delete(true);
        }
    }
}